package com.operation.net.topology;


import com.operation.net.mapper.NodeMapper;
import com.operation.net.model.CompleteConnectively;
import com.operation.net.model.Connectively;
import com.operation.net.model.InterfaceOfMac;
import com.operation.net.model.NeighborMac;
import com.operation.net.service.impl.*;
import com.operation.net.util.SNMPSessionUtil;
import org.snmp4j.PDU;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @description: 用于拓扑发现的主要类
 **/
@Component
public class TopologyDiscovery {


    @Autowired
    SystemService systemService;
    @Autowired
    InterfaceOfMacService macService;

    @Autowired
    NeighborMacService neighborMacService;
    @Autowired
    ConnectivelyService connectivelyService;

    @Autowired
    ThreadPingSuccess ping;


    public void deviceInterfaceMac() throws Exception {
        macService.clearConnectively();
        String [] macs= {"1.3.6.1.2.1.2.2.1.6"}; //mac地址oid
        String [] indexs= {"1.3.6.1.2.1.2.2.1.1"}; //接口索引号oid
        //1.获取所有活跃设备
//        List<String> activeDevices =  systemService.getAllActDevice();
        List<String> ips = systemService.getIps(); //找出表中所有的设备
        List<String> activeDevices = ping.receiveConnectSuccess(ips); //找出ping成功的设备
        for (int j =0;j<activeDevices.size();j++) {
            List<InterfaceOfMac> ofMacs = new ArrayList<>();
            SNMPSessionUtil issnmp = new SNMPSessionUtil(activeDevices.get(j), "161", "public", "2");
            if (issnmp.snmpWalk2(macs) == null||issnmp.snmpWalk2(indexs) == null){
                continue;
            }
            ArrayList<String> listMac = issnmp.snmpWalk2(macs);
            ArrayList<String> indexlist = issnmp.snmpWalk2(indexs);
            List<String> hasmac = new ArrayList<>();
            for (int i = 0; i < listMac.size(); i++) {
                String mac = listMac.get(i).substring(listMac.get(i).lastIndexOf("=")).replace("=", "").trim();
                if (hasmac.contains(mac)) continue;
                hasmac.add(mac);
                InterfaceOfMac item = new InterfaceOfMac();
                item.setIp(activeDevices.get(j));
                item.setIfindex(indexlist.get(i).substring(indexlist.get(i).lastIndexOf("=")).replace("=","").trim());
                item.setIfmac(mac);
                ofMacs.add(item);
            }
            System.out.println(ofMacs);
            macService.addMac(ofMacs);//循环遍历，把设备的每个接口的信息插入到表中
        }
    }


    public void neighborMac() throws Exception {
        neighborMacService.clearConnectively();
        String[] lldpmac = {"1.0.8802.1.1.2.1.4.1.1.5"};//邻居物理地址
//        List<String> actDevice =  systemService.getAllActDevice();
        List<String> ips = systemService.getIps();
        List<String> actDevice = ping.receiveConnectSuccess(ips);

//        System.out.println(actDevice);
        for (String ip : actDevice) {
            List<NeighborMac> neighborMacs = new ArrayList<>();
            SNMPSessionUtil issnmp = new SNMPSessionUtil(ip, "161", "public", "2");
//            System.out.println(issnmp);
            if (issnmp.snmpWalk2(lldpmac) == null||issnmp.snmpWalk2(lldpmac).size() == 0){
                continue;
            }
            List<String> lldpmacs = issnmp.snmpWalk2(lldpmac);

            System.out.println(lldpmacs);
            List<String> exist = new ArrayList<>();
            for (String item : lldpmacs) {
                String mac = item.substring(item.lastIndexOf("=")).replace("=", "").trim();
                if (exist.contains(mac)) continue;
                exist.add(mac);
                NeighborMac neighborMac = new NeighborMac();
                neighborMac.setIp(ip);
                neighborMac.setRemmac(mac);
                neighborMacs.add(neighborMac);
                System.out.println(neighborMacs);
            }

            neighborMacService.addRemMac(neighborMacs);
        }
    }


    public void connectOfAll(){
        connectivelyService.clearConnectively();
        List<Connectively> connectivelies = connectivelyService.getConnectAll(); //关联接口表和邻居接口表得到初步接口连接关系
        for (Connectively con : connectivelies) {
            Connectively connectively = new Connectively();
            connectively.setSip(con.getSip());
            connectively.setDip(con.getDip());
            connectively.setDifindex(con.getDifindex());
            System.out.println(connectively);

            if (connectivelyService.hasSip2Dip(con.getSip(),con.getDip()) && connectivelyService.hasDip2Sip(con.getDip(),con.getSip())) continue;
            connectivelyService.addConnect(connectively);  //把初步得到的接口关系存入接口表中
        }
    }


    public void initConnectively(){
        List<CompleteConnectively> connectivelies = connectivelyService.selectConnectively();  //把初步得到的连接关系进行自关联得到最后的连接关系
        connectivelyService.clearConnectively();//清空原来的连接表
        connectivelyService.insertConnectively(connectivelies);//插入最终的连接关系
    }



}
